package com.base.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wx.exception.WxException;
import com.wx.util.WxMpUtil;

import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.WxMenu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;

/**
 * 核心请求处理类
 * 
 * @author 简爱微萌
 * @Email zyw205@gmail.com
 * 
 */
public class CrazyServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 微信加密签名
		String signature = request.getParameter("signature");
		// 时间戳
		String timestamp = request.getParameter("timestamp");
		// 随机数
		String nonce = request.getParameter("nonce");
		// 随机字符串
		String echostr = request.getParameter("echostr");
		PrintWriter out = response.getWriter();
		if (WxMpUtil.getTestService().checkSignature(timestamp, nonce, signature)) {
			out.print(echostr);
		}

		try {
			// WxMpUserList userList = WxMpUtil.getTestService().userList(null);
			// System.out.println(userList);
			// WxMpUtil.getTestService().menuDelete();

			WxMenu menu = new WxMenu();

			List<WxMenu.WxMenuButton> buttons = new ArrayList<WxMenu.WxMenuButton>();
			menu.setButtons(buttons);

			WxMenu.WxMenuButton shopBtn = new WxMenu.WxMenuButton();
			shopBtn.setName("店家");
			buttons.add(shopBtn);
			List<WxMenuButton> subButtons = new ArrayList<WxMenuButton>();
			WxMenuButton sub = new WxMenuButton();
			sub.setType("view");
			sub.setName("店铺首页");
			WxMpService wxMpService = WxMpUtil.getWxMpServiceByAppName("fruits");
			sub.setUrl(wxMpService.oauth2buildAuthorizationUrl(
					"http://120.26.196.199/mobile/wxshop/info?appName=fruits", "snsapi_userinfo", null));
			subButtons.add(sub);
			shopBtn.setSubButtons(subButtons);
			WxMenu.WxMenuButton customBtn = new WxMenu.WxMenuButton();
			customBtn.setName("买家");
			buttons.add(customBtn);
			List<WxMenuButton> customerSubs = new ArrayList<WxMenuButton>();
			WxMenuButton custom_sub = new WxMenuButton();
			custom_sub.setType("view");
			custom_sub.setName("买家");
			custom_sub.setUrl(wxMpService.oauth2buildAuthorizationUrl(
					"http://120.26.196.199/mobile/wxcustomer/info?appName=fruits", "snsapi_userinfo", null));
			customerSubs.add(custom_sub);
			customBtn.setSubButtons(customerSubs);
			// WxMpUtil.getWxMpServiceByAppName("fruits").menuDelete();
			WxMpUtil.getWxMpServiceByAppName("fruits").menuCreate(menu);

		} catch (WxErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
		// if (SignUtil.checkSignature(signature, timestamp, nonce)) {
		// out.print(echostr);
		// }
		out.close();
		out = null;
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO 消息的接收、处理、响应
	}
}
